home *** CD-ROM | disk | FTP | other *** search
/ Amiga Plus 2000 #4 / Amiga Plus CD - 2000 - No. 4.iso / Tools / Text / HTML / CarcHTML / CarcHTML.c < prev    next >
C/C++ Source or Header  |  2000-05-18  |  16KB  |  511 lines

  1. /*------------------------------------------------------------
  2.  CarcHTML v1.1  Mark Weller (c)1999,2000.
  3.  Creates an HTML page for archive directories for downloading.
  4.  You can use this source code - no restrictions.
  5. --------------------------------------------------------------*/
  6.  
  7.  
  8. #include <sys/dir.h>
  9. #include <sys/types.h>
  10. #include <sys/stat.h>
  11. #include <stdio.h>
  12. #include <stdlib.h>
  13.  
  14. #define CAH_PROGRAM             "CarcHTML v1.1 - Mark Weller (c)1999,2000"
  15. #define CAH_MAXPATH         255
  16. #define CAH_MAXFILE         31
  17. #define CAH_MAXSHORT        100
  18. #define CAH_README          ".readme"
  19. #define CAH_HTMLNAME        "index.html"
  20. #define CAH_ON              1
  21. #define CAH_OFF             0
  22.  
  23. #define CAH_EXT             10
  24.  
  25. char *extension_types[] =
  26. {
  27.  "readme","README","htm","HTM","html","HTML","shtml","SHTML","info","INFO",""
  28. };
  29.  
  30. char *defbackcol[] =
  31. {
  32.  "FFFFFF",""
  33. };
  34.  
  35. int IsFileValid(char *);
  36. int IsDir(char *,char *);
  37. char *GetFileSize(char *,char *,char *);
  38. char *GetShort(char *,char *,char *);
  39. void Usage(char *);
  40. char *GetReadme(char *,char *);
  41.  
  42. void main(int argc,char *argv[])
  43. {
  44.  DIR           *dirp;
  45.  struct direct *dp;
  46.  char          *name;
  47.  
  48.  char          fsize[30];
  49.  
  50.  char gdir[CAH_MAXPATH];
  51.  char readme_file[CAH_MAXFILE];
  52.  char short_desc[CAH_MAXSHORT];
  53.  char create_dwnld[CAH_MAXPATH];
  54.  char *tback_col_1;
  55.  char *tback_col_2;
  56.  char *tback_col_3;
  57.  char *pback_col;
  58.  
  59.  int cah_result = 0;
  60.  int temp_cnt   = 0;
  61.  
  62.  FILE *download;
  63.  
  64. /*-command line switches-*/
  65.  int v_switch   = CAH_OFF; /*-verbose            -*/
  66.  int a_switch   = CAH_OFF; /*-aminet short       -*/
  67.  int b_switch   = CAH_OFF; /*-back/table colours -*/
  68.  int t_switch   = CAH_OFF; /*-page title         -*/
  69.  
  70.         gdir[0] ='\0';
  71.  
  72.         /*-set default background colours-*/
  73.  
  74.         tback_col_1 = defbackcol[0];
  75.         tback_col_2 = defbackcol[0];
  76.         tback_col_3 = defbackcol[0];
  77.         pback_col = defbackcol[0];
  78.  
  79.         /*-process command line arguments-*/
  80.  
  81.         if(argc > 8) Usage(NULL);
  82.  
  83.         switch(argc)
  84.               {
  85.                case 1 : Usage(NULL);     break;
  86.                case 2 : name = argv[1];  break;
  87.                case 3 :
  88.                case 4 :
  89.                case 7 :
  90.                case 8 :   if(argv[1][0] != '-')
  91.                             {
  92.                              Usage(NULL);
  93.                             }
  94.                           else
  95.                             {
  96.                              for(temp_cnt = 0;temp_cnt != strlen(argv[1]);temp_cnt++)
  97.                                 {
  98.                                  switch(argv[1][temp_cnt])
  99.                                        {
  100.                                         case '-' : break;
  101.                                         case 'a' : a_switch = CAH_ON; break;
  102.                                         case 'v' : v_switch = CAH_ON; break;
  103.                                         case 't' : switch(argc)
  104.                                                          {
  105.                                                           case 4  : t_switch = 3; break; /*argv[3]*/
  106.                                                           case 8  : t_switch = 7; break; /*argv[7]*/
  107.                                                           default : Usage(NULL);  break;
  108.                                                          }
  109.                                                    break;
  110.                                         case 'b' : if(argc == 7 || argc == 8)
  111.                                                      {
  112.                                                       b_switch = CAH_ON;
  113.                                                       pback_col = argv[3];
  114.                                                       tback_col_1 = argv[4];
  115.                                                       tback_col_2 = argv[5];
  116.                                                       tback_col_3 = argv[6];
  117.  
  118.                                                      }
  119.                                                     else
  120.                                                      {
  121.                                                       Usage(NULL);
  122.                                                      }
  123.                                                     break;
  124.                                        }
  125.                                 }
  126.                              name = argv[2];
  127.                             }
  128.                           break;
  129.                default :  Usage(NULL); break;
  130.               }
  131.  
  132.  
  133.     /*-open requested directory-*/
  134.  
  135.         if ((dirp = opendir(name)) == NULL) {
  136.                 Usage("Can't open directory ?");
  137.                 }
  138.  
  139.     /*-create index.html -*/
  140.         strcpy(create_dwnld,name);
  141.         AddPart(create_dwnld,CAH_HTMLNAME);
  142.  
  143.         if(!(download = fopen(create_dwnld,"w")))  /*html.file*/
  144.           {
  145.            closedir(dirp);
  146.            Usage("ERR: Can not create HTML file ?");
  147.           }
  148.     /*-create header for index.html-*/
  149.  
  150.         fprintf(download,"<HTML>\n");
  151.         fprintf(download," <HEAD>\n");
  152.         if(t_switch == CAH_OFF)
  153.           {
  154.            fprintf(download,"  <TITLE>Downloads</TITLE>\n");
  155.           }
  156.         else
  157.           {
  158.            fprintf(download,"  <TITLE>%s</TITLE>\n",argv[t_switch]);
  159.           }
  160.         fprintf(download," </HEAD>\n\n");
  161.         fprintf(download," <BODY BGCOLOR=%c%c%s%c>\n",'"','#',pback_col,'"');
  162.         fprintf(download,"  <!-- Created using %s  -->\n",CAH_PROGRAM);
  163.         fprintf(download,"  <!-- ARCHIVE INDEX STARTS HERE -->\n\n");
  164.  
  165.         if(t_switch == CAH_OFF)
  166.           {
  167.            fprintf(download,"  <H3><HR ALIGN=LEFT>Downloads</H3>\n");
  168.           }
  169.         else
  170.           {
  171.            fprintf(download,"  <H3><HR ALIGN=LEFT>%s</H3>\n",argv[t_switch]);
  172.           }
  173.         fprintf(download,"  <TABLE BORDER=0 WIDTH=100%%>\n");
  174.  
  175.         if(v_switch == CAH_ON)
  176.           {
  177.            printf("------------------------------------------------\n");
  178.            printf("%s\n",CAH_PROGRAM);
  179.            printf("------------------------------------------------\n");
  180.            printf("Archive Directory      = %s\n\n",name);
  181.  
  182.            if(t_switch == CAH_OFF)
  183.              { printf("Title = Download (default)\n"); }
  184.            else { printf("Title = %s USED\n",argv[t_switch]); }
  185.  
  186.            if(b_switch == CAH_ON)
  187.              {
  188.               printf("Page Background colour = %s USED\n",pback_col);
  189.               printf("FileName column colour = %s USED\n",tback_col_1);
  190.               printf("FileSize column colour = %s USED\n",tback_col_2);
  191.               printf("Readme   column colour = %s USED\n",tback_col_3);
  192.              }
  193.            else
  194.              {
  195.               printf("Default Colours  = USED\n");
  196.              }
  197.  
  198.           }
  199.  
  200.         /*-process archive files in directory-*/
  201.  
  202.         for (dp = readdir(dirp); dp != NULL; dp = readdir(dirp))
  203.              {
  204.               if(IsDir(name,dp->d_name) == 1)  /*-Not Sub Directory-*/
  205.                 {
  206.                  if(IsFileValid(dp->d_name) == 1)
  207.                    {
  208.                     fsize[0] ='\0';
  209.                     GetFileSize(name,dp->d_name,fsize);
  210.                     readme_file[0] = '\0';
  211.                     GetReadme(dp->d_name,readme_file);
  212.  
  213.                     if(a_switch == CAH_ON)
  214.                       {
  215.                        short_desc[0] = '\0';
  216.                        GetShort(name,readme_file,short_desc);
  217.                       }
  218.                     fprintf(download,"   <TR>\n");
  219.                     fprintf(download,"    <TD BGCOLOR=%c%c%s%c WIDTH=40%%><LEFT><A HREF=%c%s%c>%s</A></TD>\n",'"','#',tback_col_1,'"','"',dp->d_name,'"',dp->d_name);
  220.                     fprintf(download,"    <TD BGCOLOR=%c%c%s%c WIDTH=10%%><CENTER>%s</TD>\n",'"','#',tback_col_2,'"',fsize);
  221.  
  222.                     if(a_switch == CAH_ON)
  223.                       {
  224.                        if(strlen(short_desc) == 0)  /*- no short found -*/
  225.                          fprintf(download,"    <TD BGCOLOR=%c%c%s%c WIDTH=50%%><LEFT><A HREF=%c%s%c>%s</A></TD>\n",'"','#',tback_col_3,'"','"',readme_file,'"',readme_file);
  226.                       else
  227.                          fprintf(download,"    <TD BGCOLOR=%c%c%s%c WIDTH=50%%><LEFT><A HREF=%c%s%c>%s</A></TD>\n",'"','#',tback_col_3,'"','"',readme_file,'"',short_desc);
  228.                       }
  229.                     else
  230.                       {
  231.                        fprintf(download,"    <TD BGCOLOR=%c%c%s%c WIDTH=50%%><LEFT><A HREF=%c%s%c>%s</A></TD>\n",'"','#',tback_col_3,'"','"',readme_file,'"',readme_file);
  232.                       }
  233.                     fprintf(download,"   </TR>\n");
  234.  
  235.                     /*-process verbose option-*/
  236.                     if(v_switch == CAH_ON)
  237.                       {
  238.                        if(a_switch == CAH_ON)
  239.                          {
  240.                           if(strlen(short_desc) == 0)  /*- no short found -*/
  241.                              printf("%25s - %6s - %s SHORT NOT FOUND\n",dp->d_name,fsize,readme_file);
  242.                           else
  243.                              printf("%25s - %6s - %s SHORT USED\n",dp->d_name,fsize,readme_file);
  244.                          }
  245.                        else
  246.                          {
  247.                           printf("%25s - %6s - %s \n",dp->d_name,fsize,readme_file);
  248.                          }
  249.                       }
  250.                    }
  251.                 }
  252.              }
  253.  
  254.         closedir(dirp);
  255.  
  256.         if(v_switch == CAH_ON)
  257.            printf("------------------------------------------------\n");
  258.  
  259.     /*- Create end for index.html -*/
  260.         fprintf(download,"  </TABLE>\n");
  261.         fprintf(download,"  <!-- END OF INDEX ARCHIVE -->\n");
  262.         fprintf(download,"  <H3><HR ALIGN=LEFT></H3>\n");
  263.         fprintf(download," </BODY>\n");
  264.         fprintf(download,"</HTML>\n");
  265.         fclose(download);
  266.  
  267.         exit(0);
  268. }
  269.  
  270. char *GetShort(char *dir,char *readme,char *s_desc)
  271. {
  272. char temp_path[CAH_MAXPATH];
  273. char temp_sdes[CAH_MAXSHORT];
  274. char temp_sdes2[CAH_MAXSHORT];
  275.  
  276. int des_len = 0;
  277. int flag = 0;
  278. int counter = 5;
  279. FILE *read;
  280.  
  281.     temp_path[0] = '\0';
  282.     temp_sdes[0] = '\0';
  283.     temp_sdes2[0] = '\0';
  284.  
  285.     strcpy(temp_path,dir);
  286.     AddPart(temp_path,readme);
  287.  
  288.     if(!(read = fopen(temp_path,"r")))  /*text file*/
  289.       {
  290.        sprintf(s_desc,"%s",temp_sdes);
  291.       }
  292.     else
  293.       {
  294.        fgets(temp_sdes,CAH_MAXSHORT,read); /*- get first line - should be Short: -*/
  295.        fclose(read);
  296.  
  297.        /*- check for Short: -*/
  298.  
  299.         if(temp_sdes[0] == 'S' || temp_sdes[0] == 's' ) flag += 1;
  300.         if(temp_sdes[1] == 'H' || temp_sdes[1] == 'h' ) flag += 1;
  301.         if(temp_sdes[2] == 'O' || temp_sdes[2] == 'o' ) flag += 1;
  302.         if(temp_sdes[3] == 'R' || temp_sdes[3] == 'r' ) flag += 1;
  303.         if(temp_sdes[4] == 'T' || temp_sdes[4] == 't' ) flag += 1;
  304.         if(temp_sdes[5] == ':') flag += 1;
  305.  
  306.         if(flag == 6)  /*- found Short: -*/
  307.           {
  308.            flag = 0;
  309.            des_len = strlen(temp_sdes); des_len--;
  310.  
  311.            if(temp_sdes[des_len] == '\n') temp_sdes[des_len] = '\0';
  312.  
  313.            des_len = strlen(temp_sdes); des_len--;
  314.  
  315.            for(counter = 6;counter != des_len + 1;counter++)
  316.               {
  317.                temp_sdes2[counter - 6] = temp_sdes[counter];
  318.               }
  319.  
  320.            sprintf(s_desc,"%s",temp_sdes2);
  321.  
  322.           }
  323.         else
  324.           {
  325.            temp_sdes[0] = '\0';
  326.            sprintf(s_desc,"%s",temp_sdes);
  327.           }
  328.       }
  329.  
  330.  
  331. }
  332.  
  333.  
  334. char *GetReadme(char *fname,char *readme)
  335. {
  336. char temp_buff[CAH_MAXFILE];
  337. int name_len = 0;
  338. int flag = 0;
  339.  
  340.     temp_buff[0] = '\0';
  341.     strcpy(temp_buff,fname);
  342.     name_len = (strlen(temp_buff) - 1);
  343.  
  344.     while(flag != 1)
  345.          {
  346.           if(temp_buff[name_len] == '.' || name_len == 0)
  347.             {
  348.              temp_buff[name_len] = '\0';
  349.              flag = 1;
  350.             }
  351.           else
  352.             {
  353.              temp_buff[name_len] = '\0';
  354.             }
  355.           name_len--;
  356.          }
  357.  
  358.      if(name_len == -1)
  359.         sprintf(readme,"%s%s",fname,CAH_README); /*no extension*/
  360.      else
  361.         sprintf(readme,"%s%s",temp_buff,CAH_README);
  362. }
  363.  
  364. void Usage(char *mess)
  365. {
  366.  printf("----------------------------------------\n");
  367.  
  368.  if(mess)
  369.    {
  370.     printf("mess:%s\n",mess);
  371.     printf("----------------------------------------\n");
  372.    }
  373.  
  374.   printf("%s\n",CAH_PROGRAM);
  375.   printf("----------------------------------------\n");
  376.   printf("USAGE:\n");
  377.   printf(" CarcHTML <-vatb> [arch directory] [[pagecol] [col1] [col2] [col3]] [title]\n\n");
  378.   printf("options: -\n");
  379.   printf("           v   = verbose - displays files being indexed\n");
  380.   printf("           a   = aminet  - uses the 'Short:' description\n");
  381.   printf("                 in readme file instead of readme file name\n");
  382.   printf("           t   = title (page + above archive list \n");
  383.   printf("           b   = page + table background colors\n\n");
  384.   printf("example: CarcHTML -vb Work:myarchive FFFFFF 888888 FFFF00 0000FF\n");
  385.   printf("           -----------*----------\n");
  386.   printf(" This will create a HTML file called 'index.html' inside\n");
  387.   printf("  Work:myarchive, which will display a table with an entry\n");
  388.   printf("  like this:-\n\n");
  389.   printf(" myprogs.lha  1.2MB  myprogs.readme\n\n");
  390.   printf(" The size of the file will be placed in table which is auto\n");
  391.   printf("  calculated.\n");
  392.   printf(" When browsed the page color will be WHITE\n");
  393.   printf("  Column 1 = GREY Column 2 = YELLOW Column 3 = BLUE\n");
  394.   printf("           -----------*----------\n");
  395.   printf("See CarcHTML.readme for more information.\n");
  396.   printf("E-Mail: mark@kontumnam.freeserve.co.uk\n");
  397.   printf("WWW   : www.kontumnam.freeserve.co.uk\n");
  398.   printf("----------------------------------------\n");
  399.  
  400.   exit(0);
  401. }
  402.  
  403. char *GetFileSize(char *dname,char *fname,char *fsize)
  404. {
  405.  
  406. struct stat load_stat;
  407. char gfile_buff[255];
  408. int size = 0;
  409. int rem  = 0;
  410. int ssize= 0;
  411.  
  412.    gfile_buff[0]='\0';
  413.    strcpy(gfile_buff,dname);
  414.    AddPart(gfile_buff,fname);
  415.  
  416.    stat(gfile_buff,&load_stat);
  417.  
  418.    ssize = (int)load_stat.st_size;
  419.  
  420.    if(ssize < 1024)
  421.      {
  422.       sprintf(fsize,"%db",ssize);             /*bytes*/
  423.      }
  424.    else
  425.      {
  426.       size = ssize / 1024;
  427.       if(size < 1024)
  428.         {
  429.          sprintf(fsize,"%dKb",size);               /*kbytes*/
  430.         }
  431.       else
  432.         {
  433.          rem = size % 1024;
  434.          if(rem < 100) rem = 1; else rem /= 100;
  435.  
  436.          sprintf(fsize,"%d.%dMb",size / 1024,rem); /*Mbytes*/
  437.         }
  438.      }
  439.  
  440. }
  441.  
  442. int IsDir(char *dname,char *fname)
  443. {
  444.  
  445. struct stat load_stat;
  446. char gfile_buff[255];
  447.  
  448.    gfile_buff[0]='\0';
  449.    strcpy(gfile_buff,dname);
  450.    AddPart(gfile_buff,fname);
  451.  
  452.    stat(gfile_buff,&load_stat);
  453.  
  454.    if(S_ISDIR(load_stat.st_mode) != 0)
  455.      return(0);
  456.    else
  457.      return(1);
  458. }
  459.  
  460.  
  461. IsFileValid(char *fname)
  462. {
  463.  char temp_name[31];
  464.  char ext_buff[31];
  465.  int name_length = 0;
  466.  int len_cnt = 0;
  467.  int ext_cnt = 0;
  468.  int check = 0;
  469.  int result = 1;
  470.  
  471.    temp_name[0] = '\0';
  472.    ext_buff[0] = '\0';
  473.  
  474.    strcpy(temp_name,fname);
  475.    name_length = strlen(temp_name);
  476.  
  477.    /*--get file extension--*/
  478.    for(len_cnt = 0;len_cnt != name_length;len_cnt++)
  479.       {
  480.        if(temp_name[len_cnt] == '.') /*-extension start-*/
  481.          {
  482.           if(check == 1)   /*-reset because file must 'my.file.???'-*/
  483.             {
  484.              ext_buff[0] = '\0';
  485.              ext_cnt = 0;
  486.             }
  487.           else
  488.             {
  489.              check = 1;
  490.             }
  491.          }
  492.        else
  493.          {
  494.           if(check == 1) /* copy extension */
  495.             {
  496.              ext_buff[ext_cnt] = temp_name[len_cnt];
  497.              ext_cnt++;
  498.             }
  499.          }
  500.       }
  501.     ext_buff[ext_cnt] = '\0';
  502.  
  503.     ext_cnt = 0;
  504.     for(ext_cnt = 0;ext_cnt != CAH_EXT;ext_cnt++)
  505.        {
  506.         if(strcmp(ext_buff,extension_types[ext_cnt]) == 0)
  507.            result = 0;
  508.        }
  509.  return(result);
  510. }
  511.